package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class DtsAdminGoodsController {
    @Autowired
    DtsGoodsService goodsService;
    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsCategoryService categoryService;

    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        return goodsService.update(goodsAllinone);

    }


    @RequestMapping("/catAndBrand")
    public Object catAndBrand(){
        List<CatVo> catVos = categoryService.findGoodsClass();
        List<Map<String,Object>> brands = brandService.findBrand();
        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",catVos);
        data.put("brandList",brands);
        return ResponseUtil.ok(data);

    }





    @RequestMapping("/detail")
    public Object detail(Integer id){
        if (id == null){
            return AdminResponseUtil.fail();
        }
        Map<String,Object> data = goodsService.findByDetail(id);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/list")
    public Object list(
            @RequestParam(name = "page" ,required = false,defaultValue = "1") Integer page,
            @RequestParam(name = "limit" ,required = false,defaultValue = "10")Integer limit,
            String sort,String order,String goodsSn,String name){
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandsId = new ArrayList<>();

        if (subject.hasRole("品牌制造商")){
            brandsId = brandService.finById(admin.getId());
        }
        //分页查询
        IPage<DtsGoods> ipage = goodsService.findPage(page,limit,sort,order,goodsSn,name,brandsId);
        Map<String,Object> data = new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }
}
